/*
* mituner_bridge_server.h
*
* Update "DEVICE_ID" if you are not controlling the default device
*
* Copyright 2018 Microsemi Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE.txt in the project root for license information.
*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifdef ARPA_AVAILABLE
    #include <arpa/inet.h>
#endif

// HBI includes
#include "typedefs.h"
#include "chip.h"
#include "hbi.h"

// Defines
#define DEVICE_ID 0
#define PORT 5678
#define TIMEOUT_MS 5000
#define BUFFER_SZ 2048
#define C_BUFFER_SZ 4096
#define HEADER_SZ 6

// Enum/Structs
typedef enum {
   HEADER,
   COMMAND
} message_type;

typedef enum {
   S3,
   S7,
   NA
} record_type;

// Global variables
short gCharCount = 0;
hbi_handle_t gHandle;

// Function prototypes
void sigintHandler(int sig_num);
void StoreBuffer(char * const circBuff, unsigned short *writeIndex, const char *buff, unsigned short numBytes);
void GetBuffer(char * const circBuff, unsigned short *readIndex, char *buff, unsigned short numBytes);
unsigned short HbiReadReg(reg_addr_t reg);
void HbiWriteReg(reg_addr_t reg, unsigned short data);
void HbiReadBuffS(reg_addr_t reg, char * buffer, unsigned short nbBytes);
void HbiWriteBuffS(reg_addr_t reg, char * buffer, unsigned short nbBytes);
void HbiHwReadBuffS(unsigned int reg, char * buffer, unsigned short nbBytes);
short WriteRecord(record_type recType, unsigned char count, char * record);
void LoadFirmware(char * header, char * buffer);
unsigned char IsFirmwareRunning(void);
unsigned char BusySpinWait(void);
unsigned char InitFlash(void);
void EraseFlash(char * buffer);
void SaveFirmware2Flash(char * buffer);
void SaveConfig2Flash(char * buffer, unsigned char index);
void LoadFwfromFlash(char * buffer, unsigned char index);
void ParseCmd(char * header, char * cmdAns, unsigned short cmdLen);

